/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.entities;

import beans.util.ExtendedDataTableBean;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.Table;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.selection.SimpleSelection;

/**
 *
 * @author guybrush
 */
@Entity
@Table(name = "Medic", catalog = "Progetto2", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Medic.findAll", query = "Select m From Medic m;"),
    @NamedQuery(name = "Medic.findByUsernameAndPassword", query = "Select m From Medic m "
        + "WHERE m.username =  :username "
        + "AND m.password = :password;"),
    @NamedQuery(name = "Medic.findOwnPatients", query = "Select p From Medic m,Patient p "
        + "Where p.id_med = m.id "
        + "AND m.id = :id;"),
    @NamedQuery(name="Medic.findOwnWaitingPatients", query="Select Distinct p From Medic m, Patient p, Examination e "
        + "Where p.id = e.id_pat "
        + "AND p.id_med = m.id "
        + "AND e.effettuata = 0 "
        + "AND m.id = :id"),
    @NamedQuery(name="Medic.findOwnWaitingPatientCount", query="Select count(p) From Medic m, Patient p, Examination e "
        + "Where p.id = e.id_pat "
        + "AND p.id = :pid "
        + "AND p.id_med = m.id "
        + "AND e.effettuata = 0 "
        + "AND m.id = :id"),
    @NamedQuery(name="Medic.findCounters", query="Select COUNT(e.id) From Examination e,Patient p,Medic m "
        + "Where e.id_pat = p.id "
        + "AND e.scadenza > current_date "
        + "AND p.id_med = m.id "
        + "AND e.effettuata = 1 "
        + "AND e.tipo = :tipo "
        + "AND m.id = :id;"),
    @NamedQuery(name="Medic.findPerformedExam", query="Select e From Examination e,Patient p,Medic m "
        + "Where e.id_pat = p.id "
        + "AND e.scadenza > current_date "
        + "AND p.id_med = m.id "
        + "AND e.effettuata = 1 "
        + "AND e.tipo = :tipo "
        + "AND m.id = :id;")
    })
public class Medic implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id = -1;
    @Basic(optional = false)
    @Column(name = "nome")
    private String nome;
    @Basic(optional = false)
    @Column(name = "cognome")
    private String cognome;
    @Basic(optional = false)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    
    public Medic()
    {}
    public Medic(Integer id, String nome, String cognome, String username, String password)
    {
        this.id = id;
        this.nome = nome;
        this.cognome = cognome;
        this.username = username;
        this.password = password;
    }
    
    public String getCognome() {
        return cognome;
    }

    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public List<Patient> getPatientsList()
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        return em.createNamedQuery("Medic.findOwnPatients", Patient.class)
                .setParameter("id", this.getId()).getResultList();
    }
    
    public List<Patient> getWaitedPatientsList()
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        return em.createNamedQuery("Medic.findOwnWaitingPatients", Patient.class)
                .setParameter("id", this.getId()).getResultList();
    }

    public ExtendedDataTableBean getExtended()
    {
        return new ExtendedDataTableBean(id, nome, cognome, username, password);
    }
    
    public Long getVaccCount()
    {
        return getExaminationTypedCount(1);
    }
    
    public Long getProgExamCount()
    {
        return getExaminationTypedCount(2);
    }
    
    public Long getVolExamCount()
    {
        return getExaminationTypedCount(3);
    }
    
    public List<Examination> getAllExam()
    {
        ArrayList<Examination> tmp = new ArrayList<Examination>();
        for(int x=1;x<=3;x++) tmp.addAll(getTypedExamination(x));
        
        return tmp;
    }
    
    public Calendar getCalToday()
    {
        Calendar today = Calendar.getInstance();
        today.setTime(getToday());
        
        return today;
    }
    
    public Date getToday()
    {                
        return getNoTimeDate(Calendar.getInstance().getTime());
    }
    
    public Date getNoTimeDate(Date dt)
    {
        Calendar today = Calendar.getInstance(); today.setTime(dt); today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); today.set(Calendar.MILLISECOND, 0);
        
        return today.getTime();
    }
    
    private Long getExaminationTypedCount(Integer type)
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        return em.createNamedQuery("Medic.findCounters", Long.class)
                .setParameter("tipo", String.valueOf(type))
                .setParameter("id", this.getId()).getSingleResult();
    }
    
    private List<Examination> getTypedExamination(Integer type)
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        return em.createNamedQuery("Medic.findPerformedExam", Examination.class)
                .setParameter("tipo", String.valueOf(type))
                .setParameter("id", this.getId()).getResultList();
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Medic other = (Medic) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    

    @Override
    public String toString() {
        return "db.entities.Medic[ id=" + id + " ]";
    }
    
}
